<template>
  <div>
    <el-card style="width: 40%; margin: 10px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="学号" v-if="this.form.role == 0">
          <el-input v-model="form.userId" disabled></el-input>
        </el-form-item>

        <el-form-item label="员工号" v-if="this.form.role == 1">
          <el-input v-model="form.userId" disabled></el-input>
        </el-form-item>

        <el-form-item label="宿舍楼" v-if="this.form.role == 0">
          <el-input v-model="form.floor" disabled ></el-input>
        </el-form-item>

        <el-form-item label="宿舍号" v-if="this.form.role == 0">
          <el-input v-model="form.dormId" disabled ></el-input>
        </el-form-item>

        <el-form-item label="管理楼号" v-if="this.form.role == 1">
          <el-input v-model="form.manage" disabled ></el-input>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="form.phone" ></el-input>
        </el-form-item>


        <el-form-item label="密码">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
      </el-form>

      <div style="text-align: center">
   <el-button type="primary" @click="update">提交修改</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "",
  data(){
    return{
      form:{},
    }
  },
  created() {
    let str = sessionStorage.getItem("user") || "{}"
    this.form = JSON.parse(str)
  },
  methods:{
    update() {
      if (this.form.role === 0){
        request.put("/user", this.form).then(res => {
          console.log(this.form);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
            //更新session携带的user信息
            sessionStorage.setItem("user", JSON.stringify(this.form))
            this.$router.push("/person")
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
        })
      }else {
        request.put("/user/admin", this.form).then(res => {
          console.log(this.form);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
            //更新session携带的user信息
            sessionStorage.setItem("user", JSON.stringify(this.form))
            this.$router.push("/person")
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
        })
      }

    },
  }
}
</script>

<style scoped>

</style>